<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Product extends Model
{
    public const OPEN_CITY_CONFIG  = 1; // 启动城市配置
    public const CLOSE_CITY_CONFIG = 2;  // 关闭城市配置

    public const SINGLE_TYPE   = 1;    // 单规格
    public const MULTIPLE_TYPE = 2;     // 多规格

    protected $fillable = [
        'title',
        'description',
        'image',
        'order_image',
        'on_sale',
        'price',
        'citys',
    ];

//    protected $casts = [
//        'on_sale' => 'boolean', // on_sale 是一个布尔类型的字段
//    ];
    // 与服务SKU关联
    public function skus()
    {
        return $this->hasMany(ProductSku::class);
    }

    public function productsCategory()
    {
        return $this->belongsTo(ProductsCategory::class, 'product_category_id');
    }

    public function getImageUrlAttribute()
    {
        // 如果 image 字段本身就已经是完整的 url 就直接返回
        if (Str::startsWith($this->attributes['image'], ['http://', 'https://'])) {
            return $this->attributes['image'];
        }

        return \Storage::disk(config('admin.upload.disk'))->url($this->attributes['image']);
    }

    public function getOrderImageUrlAttribute()
    {
        // 如果 image 字段本身就已经是完整的 url 就直接返回
        if (Str::startsWith($this->attributes['order_image'], ['http://', 'https://'])) {
            return $this->attributes['order_image'];
        }

        return \Storage::disk('public')->url($this->attributes['order_image']);
    }

    /**
     * id 和 title 的 map
     *
     * @return array
     */
    public static function getProductAsMap()
    {
        $products = self::all();
        $map      = [];
        foreach ($products as $product) {
            $map[$product->id] = $product->title;
        }

        return $map;
    }

    public function setCitysAttribute($value)
    {
        $this->attributes['citys'] = implode(',', $value);
    }

    /**
     * 描述:指定多个查询条件 查询多个字段 支持分页
     *
     * @param $where
     * @param int   $perPage
     * @param array $orderBy
     * @param mixed $currentPage
     *
     * @return mixed
     *               created on 2019/4/27 16:39
     *               created by wangruijie
     */
    public function getAllByWhere($where, $currentPage, $perPage = 20, $orderBy = [], array $columns = ['*'])
    {
        $obj = $this->where($where);

        if (empty($orderBy) === false) {
            foreach ($orderBy as $field => $order) {
                $obj = $obj->orderBy($field, $order);
            }
        }

        return $obj->paginate($perPage, $columns, 'page', $currentPage);
    }

    /**
     * 描述:指定多个查询条件 查询多个字段 不支持分页
     *
     * @param $where
     * @param array $orderBy
     *
     * @return mixed
     *               created on 2019/4/27 16:55
     *               created by wangruijie
     */
    public function getAllByWhereNoPaginate($where, $orderBy = [], array $columns = ['*'])
    {
        $res = $this->where($where)->select($columns);

        if (empty($orderBy) === false) {
            foreach ($orderBy as $field => $order) {
                $res = $res->orderBy($field, $order);
            }
        }

        return !empty($res->get()) ? $res->get()->toArray() : [];
    }

    /**
     * 描述: 获取一条
     *
     * @param $where
     * @param array $orderBy
     *
     * @return array
     *               created on 2019/5/21 18:36
     *               created by wangruijie
     */
    public function getOneByWhere($where, $orderBy = [], array $columns = ['*'])
    {
        $res = $this->where($where)->select($columns);

        if (empty($orderBy) === false) {
            foreach ($orderBy as $field => $order) {
                $res = $res->orderBy($field, $order);
            }
        }

        return !empty($res->first()) ? $res->first()->toArray() : [];
    }
}
